package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.dao.BrandDAO;
import com.junzhiit.nengji.model.Brand;

@Repository
public class BrandDAOImpl extends AbstractBaseDAO<Brand> implements BrandDAO {

	@Override
	public List<Brand> findAll(int pageNumber, int maxPageRow, Brand brand) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from `brand` where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (brand != null) {
			if (StringUtils.isNotBlank(brand.getShopId())) {
				sql.append(" and shop_id = ?");
				params.setString(brand.getShopId());
			}
			if (StringUtils.isNotBlank(brand.getName())) {
				sql.append(" and name like ?");
				params.setString("%" + brand.getName() + "%");
			}
		}

		return queryForPagingList(sql.toString(), Brand.class, params, pageNumber, maxPageRow);
	}

	@Override
	public Integer getTotalCount(Brand brand) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from `brand` where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (brand != null) {
			if (StringUtils.isNotBlank(brand.getShopId())) {
				sql.append(" and shop_id = ?");
				params.setString(brand.getShopId());
			}
			if (StringUtils.isNotBlank(brand.getName())) {
				sql.append(" and name like ?");
				params.setString("%" + brand.getName() + "%");
			}
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public Integer getBrandExist(Brand brand) {
		StatementParameter params = new StatementParameter();
		StringBuffer sql = new StringBuffer();

		sql.append("select count(*) from `brand` where is_delete=?");
		params.setInt(IsDelete.NO.toIntValue());
		
		if (brand != null) {
			if (StringUtils.isNotBlank(brand.getShopId())){
				sql.append(" and shop_id=?");
				params.setString(brand.getShopId());
			}
			if (StringUtils.isNotBlank(brand.getName())) {
				sql.append(" and name=?");
				params.setString(brand.getName());
			}
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public String getMaxCode() {
		String sql = "select max(code) from `brand`";

		return queryForString(sql.toString());
	}

	@Override
	public List<Brand> findAlls(String shopId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select id,name from `brand` where is_delete=?");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(shopId)){
			sql.append(" and shop_id=?");
			param.setString(shopId);
		}

		return queryForList(sql.toString(), Brand.class, param);
	}
	
	@Override
	public Brand getBrand(Brand brand){
		StringBuffer sql=new StringBuffer();
		sql.append("select * from brand");
		sql.append(" where is_delete=? ");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNotBlank(brand.getName())){
			sql.append(" and name=?");
			param.setString(brand.getName());
		}
		
		if(StringUtils.isNotBlank(brand.getShopId())){
			sql.append(" and shop_id=?");
			param.setString(brand.getShopId());
		}
		
		return super.query(sql.toString(),Brand.class,param);
	}

}
